package com.cci.kangdao.ecjv.service.impl;

import com.cci.kangdao.aop.UserThreadLocal;
import com.cci.kangdao.dao.DictionariesTDao;
import com.cci.kangdao.dao.ECJVEngineTypeTMapper;
import com.cci.kangdao.dao.ECJVUserSearchTypicalCaseRecordTMapper;
import com.cci.kangdao.dao.EcjvTypicalCaseTMapper;
import com.cci.kangdao.dao.model.UserT;
import com.cci.kangdao.ecjv.dto.EcjvTypicalCaseTDto;
import com.cci.kangdao.ecjv.model.ECJVEngineTypeT;
import com.cci.kangdao.ecjv.model.ECJVUserSearchTypicalCaseRecordT;
import com.cci.kangdao.ecjv.service.ECJVTypicalCaseTService;
import com.cci.kangdao.ecjv.vo.EcjvTypicalCaseTVo;
import com.cci.kangdao.utilTool.CollectionUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.Collator;
import java.util.*;

/**
 * @description:
 * @author: joe
 * @company: CTY
 * @since: 2022/10/24 13:57
 * @version: V1.0
 **/
@Service
public class ECJVTypicalCaseTServiceImpl implements ECJVTypicalCaseTService {
    private Logger log = Logger.getLogger(this.getClass().getName());
    @Resource
    private EcjvTypicalCaseTMapper ecjvTypicalCaseTMapper;
    @Resource
    private ECJVUserSearchTypicalCaseRecordTMapper ecjvUserSearchTypicalCaseRecordTMapper;
    @Resource
    private DictionariesTDao dictionariesTDao;
    @Resource
    private ECJVEngineTypeTMapper ecjvEngineTypeTMapper;

    @Override
    public PageInfo getList(EcjvTypicalCaseTDto ecjvTypicalCaseTDto) {

        Integer page = ecjvTypicalCaseTDto.getPage();
        Integer pageSize = ecjvTypicalCaseTDto.getPageSize();
        if (null == page) {
            ecjvTypicalCaseTDto.setPage(1);
        }
        if (null == pageSize) {
            ecjvTypicalCaseTDto.setPageSize(20);
        }
        //添加查询记录
        if (StringUtils.isNotBlank(ecjvTypicalCaseTDto.getCaseName())) {
            UserT userT = UserThreadLocal.get();
            ECJVUserSearchTypicalCaseRecordT recordsearch = new ECJVUserSearchTypicalCaseRecordT();
            recordsearch.setSearchKeyWord(ecjvTypicalCaseTDto.getCaseName());
            recordsearch.setUserId(userT.getId());
            List<ECJVUserSearchTypicalCaseRecordT> ecjvUserSearchTypicalCaseRecordTS = ecjvUserSearchTypicalCaseRecordTMapper.selectList(recordsearch);
            if (CollectionUtils.isEmpty(ecjvUserSearchTypicalCaseRecordTS)) {
                //添加
                ECJVUserSearchTypicalCaseRecordT recordT = new ECJVUserSearchTypicalCaseRecordT();
                recordT.setUserId(userT.getId());
                recordT.setSearchKeyWord(ecjvTypicalCaseTDto.getCaseName());
                recordT.setSystime(new Date());
                int insert = ecjvUserSearchTypicalCaseRecordTMapper.insertSelective(recordT);
            }
        }
        //查询集合
        PageHelper.startPage(ecjvTypicalCaseTDto.getPage(), ecjvTypicalCaseTDto.getPageSize());
        List<EcjvTypicalCaseTVo> list = ecjvTypicalCaseTMapper.findTypicalCaseList(ecjvTypicalCaseTDto);
        if (CollectionUtils.isNotEmpty(list)) {
            List<Map<String, Object>> ecjvCusComplaints = dictionariesTDao.getDictionariesListByDTableName("ecjvCusComplaints");
            Map<Integer, String> map = new HashMap<>();
            for (Map<String, Object> ecjvCusComplaint : ecjvCusComplaints) {
                map.put(Integer.valueOf(ecjvCusComplaint.get("DInt").toString()), ecjvCusComplaint.get("DValue").toString());
            }
            for (EcjvTypicalCaseTVo ecjvTypicalCaseTVo : list) {
                String cusComplaints = ecjvTypicalCaseTVo.getCusComplaints();
                List<String> stringList = new ArrayList<>();
                if (StringUtils.isNotBlank(cusComplaints)) {
                    String[] split = cusComplaints.split(",");
                    ArrayList<String> arrayList = new ArrayList<String>(split.length);
                    Collections.addAll(arrayList, split);
                    if (CollectionUtils.isNotEmpty(arrayList)) {
                        arrayList.sort((o1, o2) -> {
                            Comparator<Object> com = Collator.getInstance(Locale.CHINA);
                            return com.compare(o1, o2);
                        });
                        for (String s : arrayList) {
                            if (StringUtils.isNotBlank(s)) {
                                if (map.containsKey(Integer.valueOf(s))) {
                                    stringList.add(map.get(Integer.valueOf(s)));
                                }
                            }
                        }
                    }
                }
                ecjvTypicalCaseTVo.setCusComplaintStr(stringList);
            }
        }
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }

    @Override
    public List<ECJVUserSearchTypicalCaseRecordT> TypicalCaseRecord() {
        UserT userT = UserThreadLocal.get();
        List<ECJVUserSearchTypicalCaseRecordT> ecjvUserSearchTypicalCaseRecordTS = ecjvUserSearchTypicalCaseRecordTMapper.selectListTen(userT.getId());
        return ecjvUserSearchTypicalCaseRecordTS;
    }

    @Override
    public List<ECJVEngineTypeT> getEngineTypeList() {
        return ecjvEngineTypeTMapper.selectList();
    }
}
